﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class DangKi : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    SqlConnection con2 = new SqlConnection();
    SqlCommand com = new SqlCommand();
    SqlCommand com2 = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
       
       
        
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (TextBox1.Text.Length >= 6 && TextBox1.Text.Length <= 30)
        {
            args.IsValid = true;
        }
        else
            args.IsValid = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }
        int gt = 1;
        CaptchaControl1.ValidateCaptcha(TextBox10.Text.Trim());
        if (!CaptchaControl1.UserValidated)
        {
            Label11.ForeColor = Color.Red;
            Label11.Text = "Mã bảo vệ chưa chính xác";
            return;
        }
        else
        {
            Label11.ForeColor = Color.Green;
            Label11.Text = "Chính xác";
        }
        if (RadioButton1.Checked == false && RadioButton2.Checked == false)
        {
            Label12.Text = "Bạn chưa chọn giới tính!";
            return;
        }
        else
        {
            if (RadioButton1.Checked == true)
                gt = 1;
            else
                gt = 0;
        }
        com.Connection = con;
        com.CommandText = "select * from KhachHang where Username='" + TextBox1.Text + "'";
        con.Open();
        dr = com.ExecuteReader();
        if (dr.HasRows)
        {
            Label9.Text = " tài khoản đã sử dụng"; 
        }
        else
        {
            con2.ConnectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            com2.Connection = con2;
            com2.CommandText = "insert into KhachHang(HoTen,DT,DiaChi,Sex,NgaySinh,Email,Username,Password) values(@HoTen,@DT,@DiaChi,@Sex,@NgaySinh,@Email,@Username,@Password)";
            com2.Parameters.AddWithValue("@DT", TextBox1.Text);
            com2.Parameters.AddWithValue("@Ho", TextBox4.Text);
            com2.Parameters.AddWithValue("@NgaySinh", Convert.ToDateTime(TextBox8.Text).ToString("yyyy/MM/dd"));
            com2.Parameters.AddWithValue("@GioiTinh", gt);
            com2.Parameters.AddWithValue("@DiaChi", TextBox6.Text);
            com2.Parameters.AddWithValue("@TaiKhoan", TextBox1.Text);
            com2.Parameters.AddWithValue("@Pass", TextBox7.Text);
            com2.Parameters.AddWithValue("@Email", TextBox3.Text);
           
 
        }
        con.Close();    
    }
    
}